<template>
  <div class="app-container">
    <div
      style="display: flex; align-items: center; justify-content: space-between"
    >
      <el-space>
        <el-button type="primary" @click="handleBack">返回</el-button>
        <div style="font-size: 20px; font-weight: bold">下架商品</div>
      </el-space>
      <el-space>
        <el-text v-if="siteLength">已选择{{ siteLength }}个站点</el-text>
        <el-button type="primary" @click="choseSite" style="margin-right: 20px"
          >选择站点</el-button
        >
        <el-button type="primary" @click="handleRelease">下架商品</el-button>
      </el-space>
    </div>

    <el-table
      border
      :data="productList"
      style="margin: 20px 0; height: calc(100vh - 300px)"
    >
      <el-table-column
        label="商品图"
        align="center"
        width="200"
      >
        <template #default="scope">
          <image-preview :src="scope.row.newImage" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column
        label="商品类型"
        align="center"
        prop="productCategoryFullName"
      />
      <el-table-column label="商品名称" prop="productName" />
      <el-table-column
        label="来源网站"
        align="center"
        prop="collectionSiteName"
      />
      <el-table-column label="检测结果" prop="checkResult" />
      <el-table-column label="操作" align="center" width="100" fixed="right">
        <template #default="scope">
          <el-button link type="danger" @click="handleDelete(scope.row,scope.$index)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      :limit="queryParams.pageSize"
      :page="queryParams.pageNum"
      @pagination="handlePageChange"
    ></Pagination>

    <publicSite
      :dialogVisible="dialogVisible"
      :newType="false"
      @handleListClose="handleListClose"
      @handleListSure="handleListSure"
      :listItem="productRelease"
    ></publicSite>
  </div>
</template>

<script setup name="ProductPublish">
import publicSite from "@/views/publicSite";
import { ref, reactive, onMounted } from "vue";
import { useRouter, useRoute } from "vue-router";

const route = useRoute();
const router = useRouter();
const dialogVisible = ref(false);
const { proxy } = getCurrentInstance();
const total = ref(0);
const siteLength = ref(0);

const productList = ref([]);
const productRelease = ref({
  productIds: [],
  websiteIds: [],
  takeDownCode:0
}); //发布产品参数

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  param: {
    productIds: [], //产品名称
  },
});

onMounted(() => {
  getList();
});
const getList = () => {
  let newListIds = JSON.parse(sessionStorage.getItem("productListIds"));
  if (Array.isArray(newListIds) && newListIds.length) {
    queryParams.value.param.productIds = newListIds;
  } else {
    handleBack();
    proxy.$modal.msgWarning("请在产品中心重新勾选");
    return;
  }
  product.product
    .productGettobepublishedproductlist({
      data: queryParams.value,
    })
    .then((res) => {
      total.value = res.total;
      if (res.data?.length) {
        res.data = res.data.map((e) => {
          return {
            ...e,
            newImage: handleImage(e),
          };
        });
      }
      productList.value = res.data;
    })
};
// 删除
const handleDelete = (rowItem,index) => {
  proxy.$modal.confirm("是否确认删除此数据？").then(() => {
    var newListIds = JSON.parse(sessionStorage.getItem("productListIds"));
    var ids = newListIds.findIndex((e) => e === rowItem.id);
    newListIds.splice(ids, 1);
    sessionStorage.setItem("productListIds", JSON.stringify(newListIds));
    delete productList.value[index];
    total.value = total.value - 1;
    if(!total.value) return getList();
  });
};
// 发布商品
const handleRelease = () => {
  if(!productRelease.value.websiteIds.length) return proxy.$modal.msgWarning("请选择发布的站点");
  productRelease.value.productIds=queryParams.value.param.productIds
  proxy.$modal.confirm("是否确认下架商品？").then(() => {
  product.product.productTakedown({
    data: productRelease.value
  }).then(res=>{
    if(res.code!==200) return
    handleBack();
  })
})
};
// 站点选择确定
const handleListSure = (list) => {
  dialogVisible.value = false;
  siteLength.value = list.length;
  productRelease.value.websiteIds = list;
  console.log(productRelease.value.websiteIds);
};

// 取消站点显示
const handleListClose = () => {
  dialogVisible.value = false;
};
const choseSite = () => {
  console.log(productRelease.value.websiteIds);
  dialogVisible.value = true;
};

const handleBack = () => {
  productRelease.value.websiteIds = [];
  sessionStorage.removeItem("productListIds");
  router.back();
};
// 分页
const handlePageChange = (newPage) => {
  queryParams.value.pageNum = newPage.page;
  queryParams.value.pageSize = newPage.limit;
  getList();
};
const handleImage = (row) => {
  var imageExtensionsRegex =/^https?:\/\//i;
  if (!imageExtensionsRegex.test(row.mainImage)) {
    return row.host;
  } else {
    return row.mainImage;
  }
};
</script>
